package com.time.hellotime.common.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.time.hellotime.app.MyApplication;
import com.time.hellotime.components.MainActivity;
import com.yanzhenjie.nohttp.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

/**
 * 极光推送的接收器
 * Created by Bing on 2016/9/6.
 * E-mail is tx16bing@163.com
 */
public class JPushReceiver extends BroadcastReceiver {

    private String extras;
    private JSONObject extrasJson;
    private String type = "-1";
    private JpushBean.Content mContent;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (MyApplication.getInstance() == null) {
            return;
        }
        Bundle bundle = intent.getExtras();
        Logger.d("[ Time person JPushReceiver ] " + intent.getAction() + ", extras: " + printBundle(bundle));
        extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        if (null != extras) {
            try {
                extrasJson = new JSONObject(extras);
                type = extrasJson.getString("type");
                String content = extrasJson.getString("content");
                mContent = new Gson().fromJson(content, JpushBean.Content.class);

            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                Logger.d("[ Time person JPushReceiver ] " + intent.getAction() + ", extras: " + printBundle(bundle));
            }
        }

        if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction()) ||
                JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Logger.i("JPushReceiver 用户点击接收了通知 type is : " + type);

            switch (type) {

                case "msg":
                    break;

                case "notify":
                    break;

                default:
                    break;
            }

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Logger.e("JPushReceiver 用户点击打开了通知");

            switch (type) {

                case "msg":
                    Intent mainIntent = new Intent(context, MainActivity.class);
                    mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    context.startActivity(intent);
                    break;

                case "notify":
                    break;

                default:
                    break;
            }

        }
    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
                    Logger.i("This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Logger.e("Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

}
